package com.seventh.parkingrent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seventh.parkingrent.controller.form.RentMessageForm;
import com.seventh.parkingrent.dao.ParkingSpaceMapper;
import com.seventh.parkingrent.domain.ParkingSpace;
import com.seventh.parkingrent.domain.RentMessage;
import com.seventh.parkingrent.dao.RentMessageMapper;
import com.seventh.parkingrent.domain.Vo.UpdateStatusByidVo;
import com.seventh.parkingrent.service.IRentMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 订单详情记录表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@Service
@Slf4j
public class RentMessageServiceImpl extends ServiceImpl<RentMessageMapper, RentMessage> implements IRentMessageService {

    @Resource
    private RentMessageMapper rentMessageMapper;
    @Autowired
    private ParkingSpaceServiceImpl parkingSpaceService;

    @Override
    public List<RentMessage> getRentByPSId(List<ParkingSpace> parkingSpaceList) {
        log.info("根据停车位id查询订单");
        ArrayList<RentMessage> rentMessages = new ArrayList<>();
        for (ParkingSpace parkingSpace : parkingSpaceList) {
            Integer id = parkingSpace.getParkingSpaceId();
            QueryWrapper<RentMessage> wrapper = new QueryWrapper<>();
            wrapper.eq("spacing_space_id",id);
            List<RentMessage> rentMessages1 = rentMessageMapper.selectList(wrapper);
            for (RentMessage rentMessage : rentMessages1) {
                //如果订单是未出租成的订单
                if (rentMessage.getStatus().equals("0")){
                    rentMessages.add(rentMessage);
                }
            }
        }
        return rentMessages;
    }

    @Override
    public List<RentMessage> getAllNotRent() {
        List<RentMessage> rentMessages = rentMessageMapper.selectList(new QueryWrapper<RentMessage>());
        Iterator<RentMessage> iterator = rentMessages.iterator();
        while (iterator.hasNext()) {
            RentMessage next = iterator.next();
            if (!next.getStatus().equals("0")){
                iterator.remove();
            }
        }
        return rentMessages;
    }

    @Override
    public boolean updateStatusById(Integer id, Integer tenantryid) {
        //修改车位状态
        RentMessage rentMessage = rentMessageMapper.selectById(id);
        parkingSpaceService.updateStatusById(rentMessage.getRentMessageId());
        //修改订单状态以及租用人
        UpdateStatusByidVo vo = new UpdateStatusByidVo(id, tenantryid);
        int i = rentMessageMapper.updateStatusByid(vo);
        return i>0?true:false;
    }

    @Override
    public boolean getStatusById(Integer id) {
        RentMessage rentMessage = rentMessageMapper.selectById(id);
        String status = rentMessage.getStatus();
        return status.equals("0");
    }

    @Override
    public void okStatusById(Integer id) {
        RentMessage rentMessage = rentMessageMapper.selectById(id);
        if (!rentMessage.getStatus().equals("2")) {
            rentMessage.setStatus("2");
            rentMessageMapper.updateById(rentMessage);
        }
    }

    @Override
    public List<RentMessage> selectRentByPSId(Integer id) {
        ArrayList<RentMessage> rentMessages = new ArrayList<>();
        QueryWrapper<RentMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("spacing_space_id",id);
        List<RentMessage> rentMessages1 = rentMessageMapper.selectList(wrapper);
        for (RentMessage rentMessage : rentMessages1) {
            //如果订单是未出租成的订单
            if (rentMessage.getStatus().equals("0")){
                rentMessages.add(rentMessage);
            }
        }
        return rentMessages;
    }

    @Override
    public Integer addOrder(RentMessage rentMessage) {
        Integer insert = rentMessageMapper.insert(rentMessage);
        return insert;
    }

    @Override
    public IPage<RentMessageForm> findRent(int lessorId, int pageSize, int current) {
        log.info("{},{},{}",lessorId,pageSize,current);
        IPage<RentMessageForm> page = new Page<>( current,pageSize);
        IPage<RentMessageForm> formIPage = rentMessageMapper.findRent(page, lessorId);

        return formIPage;
    }

    @Override
    public List<RentMessage> getallleaseByid(Integer userid) {
        QueryWrapper<RentMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("tenantry_id",userid);
        List<RentMessage> rentMessages = rentMessageMapper.selectList(wrapper);
        Iterator<RentMessage> iterator = rentMessages.iterator();
        while (iterator.hasNext()) {
            RentMessage next = iterator.next();
            if (!next.getStatus().equals("2")){
                iterator.remove();
            }
        }
        return rentMessages;
    }

    @Override
    public RentMessage getRentById(Integer rentid) {
        return rentMessageMapper.selectById(rentid);
    }


///H
    @Override
    public List<RentMessage> findOrderTime(int spacingSpaceId) {
        List<RentMessage> list=rentMessageMapper.findOrderTime(spacingSpaceId);

        return list;
    }

    @Override
    public boolean deleteRow(int rentMessageId) {
        log.info("{}",rentMessageId);
        int row = rentMessageMapper.deleteById(rentMessageId);
        return row>0?true:false;
    }

    @Override
    public List<RentMessageForm> findById(int rentMessageId) {
        return rentMessageMapper.findById(rentMessageId);
    }


    @Override
    public boolean okStatusAndtenantryId(Integer id, Integer tenantryid) {
        RentMessage rentMessage = rentMessageMapper.selectById(id);
        parkingSpaceService.updateStatusById(rentMessage.getSpacingSpaceId());
        rentMessage.setStatus("2");
        rentMessage.setTenantryId(tenantryid);
        int i = rentMessageMapper.updateById(rentMessage);
        return i>0;
    }


}
